package son.vpoint.dinokids;

import java.io.IOException;
import java.text.BreakIterator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

import org.anddev.andengine.util.MathUtils;




import son.vpoint.dinokids.until.MathFactory;
import son.vpoint.dinokids.view.GameView;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AbsoluteLayout;
import android.widget.AbsoluteLayout.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class GameActivity extends Activity implements OnTouchListener, OnClickListener {


	private int status = -1;
	private int count1 = 0, count2 = 0;
	private ImageView[] arrImageItems;
	private String srtHienthimanchoi = "";

	private Integer[] arrX;
	private Integer[] arrY;
	public int DEVICE_WIDTH;
	public int DEVICE_HEIGHT;
	public static int x, x1;
	public static int y, y1;
	public int myScore = 0;
	
	private OperatorSelected mOperatorSelected;
	private GameActivity mGameActivity;
	public GameView mGameView;
	public MathFactory mFactory;
	public static boolean onCorrect = false;
	public static boolean oninCorrect = false;

	public static LayoutParams lp;
	private RelativeLayout rlbackhome;
	private AbsoluteLayout mode1_layout;
	
	private LinearLayout lnParam1;
	private LinearLayout lnParam2;
	private LinearLayout lnParam3;
	private LinearLayout lnResult;
	
	private TextView txtHienthimanchoi;
	private TextView txtScore;
	private TextView txtTodayBestscore;
	private TextView txtOperator1;
	private TextView txtOperator2;
	private TextView txtBang;
	private TextView txtParam1;
	private TextView txtParam2;
	private TextView txtParam3;
	private TextView txtResult;
	private Button btnback;
	


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.maingame);

		mOperatorSelected = new OperatorSelected();
		mGameActivity = new GameActivity();
		mFactory = new MathFactory();
		DisplayMetrics mDisplayMetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics);
		Typeface mFontHienthimanchoi = Typeface.createFromAsset(getAssets(),
				"fonts/vbookmi.TTF");
		Typeface mFontSo = Typeface.createFromAsset(getAssets(),
				"fonts/vnibaybuom.ttf");
		txtHienthimanchoi = (TextView) findViewById(R.id.txthienthimanchoi);
		txtHienthimanchoi.setTextColor(Color.WHITE);
		txtHienthimanchoi.setTypeface(mFontHienthimanchoi);
		txtHienthimanchoi.setTextSize(60);
		txtHienthimanchoi.setText(""+ this.getTextBoard(mFactory.mOperator, srtHienthimanchoi));

		txtScore = (TextView) findViewById(R.id.txtMyScore);
		txtScore.setTypeface(mFontHienthimanchoi);
		txtScore.setText(""+myScore);
		txtTodayBestscore = (TextView) findViewById(R.id.txtTodayBest);
		btnback = (Button) findViewById(R.id.btnback);
		btnback.setOnClickListener(this);
		
		DEVICE_WIDTH = mDisplayMetrics.widthPixels;
		DEVICE_HEIGHT = mDisplayMetrics.heightPixels;

		x = DEVICE_WIDTH;
		y = DEVICE_HEIGHT;
		x1 = x / 10;
		y1 = 3 * y / 4;
		arrX = new Integer[] { x1, 2 * x1 - x1 / 6, 3 * x1 - x1 / 3,
				4 * x1 - x1 / 2, 4 * x1 + x1 / 2, 5 * x1 + x1 / 2,
				6 * x1 + x1 / 2, 7 * x1 + x1 / 2, 8 * x1 + x1 / 2 };
		arrY = new Integer[] { y1 - 30, y1 - 30, y1 - 30, y1 - 30, y1 - 30,
				y1 - 30, y1 - 30, y1 - 30, y1 - 30 };

		mGameView = new GameView(GameActivity.this);
		mode1_layout = (AbsoluteLayout) findViewById(R.id.Almaingame);
		mode1_layout.addView(mGameView);
		arrImageItems = new ImageView[9];

		arrImageItems[0] = (ImageView) findViewById(R.id.imgSo1);
		arrImageItems[0].setBackgroundResource(R.drawable.so1);
		arrImageItems[1] = (ImageView) findViewById(R.id.imgSo2);
		arrImageItems[1].setBackgroundResource(R.drawable.so2);
		arrImageItems[2] = (ImageView) findViewById(R.id.imgSo3);
		arrImageItems[2].setBackgroundResource(R.drawable.so3);
		arrImageItems[3] = (ImageView) findViewById(R.id.imgSo4);
		arrImageItems[3].setBackgroundResource(R.drawable.so4);
		arrImageItems[4] = (ImageView) findViewById(R.id.imgSo5);
		arrImageItems[4].setBackgroundResource(R.drawable.so5);
		arrImageItems[5] = (ImageView) findViewById(R.id.imgSo6);
		arrImageItems[5].setBackgroundResource(R.drawable.so6);
		arrImageItems[6] = (ImageView) findViewById(R.id.imgSo7);
		arrImageItems[6].setBackgroundResource(R.drawable.so7);
		arrImageItems[7] = (ImageView) findViewById(R.id.imgSo8);
		arrImageItems[7].setBackgroundResource(R.drawable.so8);
		arrImageItems[8] = (ImageView) findViewById(R.id.imgSo9);
		arrImageItems[8].setBackgroundResource(R.drawable.so9);
		
		lnParam1=(LinearLayout)findViewById(R.id.lnl_param1);
		lnParam2=(LinearLayout)findViewById(R.id.lnl_param2);
		lnParam3=(LinearLayout)findViewById(R.id.lnl_param3);
		lnResult=(LinearLayout)findViewById(R.id.lnl_Result);
		
		txtOperator1=(TextView)findViewById(R.id.txtOperator1);
		txtOperator2=(TextView)findViewById(R.id.txtOperator2);
		txtBang=(TextView)findViewById(R.id.txtBang);
		txtParam1=(TextView)findViewById(R.id.txt_param1);
		
		txtParam1.setTypeface(mFontSo);
		txtParam1.setTextColor(Color.WHITE);
		txtParam2=(TextView)findViewById(R.id.txt_param2);
		txtParam2.setTypeface(mFontSo);
		txtParam2.setTextColor(Color.WHITE);
		txtParam3=(TextView)findViewById(R.id.txt_param3);
		txtParam3.setTypeface(mFontSo);
		txtParam3.setTextColor(Color.WHITE);
		
		txtResult=(TextView)findViewById(R.id.txt_Result);
		txtResult.setTypeface(mFontSo);
		txtResult.setTextColor(Color.WHITE);
		rlbackhome = (RelativeLayout) findViewById(R.id.rlbackhome);
		onDraw();
		nextQuestion();
	
		mode1_layout.setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {

				// TODO Auto-generated method stub
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					v.bringToFront();
				}
				if (status != -1) {
					
					lp = new AbsoluteLayout.LayoutParams(
							android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
							android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
							(int) event.getX() - arrImageItems[0].getWidth()
									/ 2, (int) event.getY()
									- arrImageItems[0].getHeight() / 2);
					arrImageItems[status].setLayoutParams(lp);

				}
				if (event.getAction() == MotionEvent.ACTION_UP) {
					int a = (int) event.getX();
					int b = (int) event.getY();
					if (status != -1) {
						switch (mFactory.getDifficultLevel()) {
						case 0:
							if (mFactory.mOperator != 3) {
								if(mFactory.mOperatorMix!=4){
								if (mFactory.getParam1() == -1) {

									if (a > 196 * DEVICE_WIDTH / 1280
											&& a < 350 * DEVICE_WIDTH / 1280
											&& b > 330 * DEVICE_HEIGHT / 800
											&& b < 415 * DEVICE_HEIGHT / 800) {
										mGameActivity.setLayoutParamView(lp,
												arrImageItems[status],
												196 * DEVICE_WIDTH / 1280,
												330 * DEVICE_HEIGHT / 800);
										txtParam1.setBackgroundDrawable(null);
										checkOperator(mFactory.mOperator);

									} else {
										// Chay ve cho cu
										mGameActivity.setLayoutParamView(lp,
												arrImageItems[status],
												arrX[status], arrY[status]);

										status = -1;
										txtParam1.setBackgroundResource(R.drawable.dau_hoi);
									}

								}
								if (mFactory.getParam2() == -1) {
									if (a > 535 * DEVICE_WIDTH / 1280
											&& a < 605 * DEVICE_WIDTH / 1280
											&& b > 330 * DEVICE_HEIGHT / 800
											&& b < 415 * DEVICE_HEIGHT / 800) {
										mGameActivity.setLayoutParamView(lp,
												arrImageItems[status],
												535 * DEVICE_WIDTH / 1280,
												330 * DEVICE_HEIGHT / 800);
										txtParam2.setBackgroundDrawable(null);
										checkOperator(mFactory.mOperator);

									} else {
										// Chay ve cho cu
										mGameActivity.setLayoutParamView(lp,
												arrImageItems[status],
												arrX[status], arrY[status]);

										status = -1;
										txtParam2.setBackgroundResource(R.drawable.dau_hoi);

									}
								}
								}else{
									if (mFactory.getParam2() == -1) {
										if (a > 535 * DEVICE_WIDTH / 1280
												&& a < 605 * DEVICE_WIDTH / 1280
												&& b > 330 * DEVICE_HEIGHT / 800
												&& b < 415 * DEVICE_HEIGHT / 800) {
											mGameActivity.setLayoutParamView(lp,
													arrImageItems[status],
													535 * DEVICE_WIDTH / 1280,
													330 * DEVICE_HEIGHT / 800);
											txtParam2.setBackgroundDrawable(null);
											checkOperator(mFactory.mOperator);

										} else {
											// Chay ve cho cu
											mGameActivity.setLayoutParamView(lp,
													arrImageItems[status],
													arrX[status], arrY[status]);
											txtParam2.setBackgroundResource(R.drawable.dau_hoi);


											status = -1;
										}
									}

									if (mFactory.getResult() == -1) {
										if (a > 950 * DEVICE_WIDTH / 1280
												&& a < 1130 * DEVICE_WIDTH / 1280
												&& b > 330 * DEVICE_HEIGHT / 800
												&& b < 415 * DEVICE_HEIGHT / 800) {
											mGameActivity.setLayoutParamView(lp,
													arrImageItems[status],
													950 * DEVICE_WIDTH / 1280,
													330 * DEVICE_HEIGHT / 800);
											txtResult.setBackgroundDrawable(null);
											checkOperator(mFactory.mOperator);

										} else {
											// Chay ve cho cu
											mGameActivity.setLayoutParamView(lp,
													arrImageItems[status],
													arrX[status], arrY[status]);
											txtResult.setBackgroundResource(R.drawable.dau_hoi);


											status = -1;
										}
									
								}
								}
								} else 
								{
								if (mFactory.getParam2() == -1) {
									if (a > 535 * DEVICE_WIDTH / 1280
											&& a < 605 * DEVICE_WIDTH / 1280
											&& b > 330 * DEVICE_HEIGHT / 800
											&& b < 415 * DEVICE_HEIGHT / 800) {
										mGameActivity.setLayoutParamView(lp,
												arrImageItems[status],
												535 * DEVICE_WIDTH / 1280,
												330 * DEVICE_HEIGHT / 800);
										txtParam2.setBackgroundDrawable(null);
										checkOperator(mFactory.mOperator);

									} else {
										// Chay ve cho cu
										mGameActivity.setLayoutParamView(lp,
												arrImageItems[status],
												arrX[status], arrY[status]);
										

										status = -1;
										txtParam2.setBackgroundResource(R.drawable.dau_hoi);

									}
								}

								if (mFactory.getResult() == -1) {
									if (a > 950 * DEVICE_WIDTH / 1280
											&& a < 1130 * DEVICE_WIDTH / 1280
											&& b > 330 * DEVICE_HEIGHT / 800
											&& b < 415 * DEVICE_HEIGHT / 800) {
										mGameActivity.setLayoutParamView(lp,
												arrImageItems[status],
												950 * DEVICE_WIDTH / 1280,
												330 * DEVICE_HEIGHT / 800);
										txtResult.setBackgroundDrawable(null);
										checkOperator(mFactory.mOperator);

									} else {
										// Chay ve cho cu
										mGameActivity.setLayoutParamView(lp,
												arrImageItems[status],
												arrX[status], arrY[status]);

										status = -1;
										txtResult.setBackgroundResource(R.drawable.dau_hoi);

									}

								}
							}

							break;
						case 1:
							if(mFactory.mOperator!=3){
							if(mFactory.mOperatorMix!=4){
							if ((a > 196 * DEVICE_WIDTH / 1280
									&& a < 350 * DEVICE_WIDTH / 1280
									&& b > 330 * DEVICE_HEIGHT / 800 && b < 415 * DEVICE_HEIGHT / 800)
									|| (a > 535 * DEVICE_WIDTH / 1280
											&& a < 610 * DEVICE_WIDTH / 1280
											&& b > 330 * DEVICE_HEIGHT / 800 && b < 415 * DEVICE_HEIGHT / 800)) 
							{
								if (a > 196 * DEVICE_WIDTH / 1280
										&& a < 350 * DEVICE_WIDTH / 1280
										&& b > 330 * DEVICE_HEIGHT / 800
										&& b < 415 * DEVICE_HEIGHT / 800) {
									mGameActivity.setLayoutParamView(lp,
											arrImageItems[status],
											196 * DEVICE_WIDTH / 1280,
											330 * DEVICE_HEIGHT / 800);
									txtParam1.setBackgroundDrawable(null);
									count1 = status + 1;
									Log.d("" + count1, "count1");

								}

								if (a > 535 * DEVICE_WIDTH / 1280
										&& a < 610 * DEVICE_WIDTH / 1280
										&& b > 330 * DEVICE_HEIGHT / 800
										&& b < 415 * DEVICE_HEIGHT / 800) {
									mGameActivity.setLayoutParamView(lp,
											arrImageItems[status],
											535 * DEVICE_WIDTH / 1280,
											330 * DEVICE_HEIGHT / 800);
									count2 = status + 1;
									txtParam2.setBackgroundDrawable(null);
									Log.d("" + count2, "count2");
								}

								checkOperator(mFactory.mOperator);

							}
							}else{
								if (a > 535 * DEVICE_WIDTH / 1280
										&& a < 620 * DEVICE_WIDTH / 1280
										&& b > 330 * DEVICE_HEIGHT / 800
										&& b < 435 * DEVICE_HEIGHT / 800
										|| a > 940 * DEVICE_WIDTH / 1280
										&& a < 1020 * DEVICE_WIDTH / 1280
										&& b > 330 * DEVICE_HEIGHT / 800
										&& b < 435 * DEVICE_HEIGHT / 800) {
									if (a > 535 * DEVICE_WIDTH / 1280
											&& a < 620 * DEVICE_WIDTH / 1280
											&& b > 330 * DEVICE_HEIGHT / 800
											&& b < 435 * DEVICE_HEIGHT / 800) {
										mGameActivity.setLayoutParamView(lp,
												arrImageItems[status],
												535 * DEVICE_WIDTH / 1280,
												345 * DEVICE_HEIGHT / 800);
										count1 = status + 1;
										txtParam2.setBackgroundDrawable(null);
									}
									if (a > 940 * DEVICE_WIDTH / 1280
											&& a < 1020 * DEVICE_WIDTH / 1280
											&& b > 330 * DEVICE_HEIGHT / 800
											&& b < 435 * DEVICE_HEIGHT / 800) {
										mGameActivity.setLayoutParamView(lp,
												arrImageItems[status],
												940 * DEVICE_WIDTH / 1280,
												345 * DEVICE_HEIGHT / 800);
										count2 = status + 1;
										txtResult.setBackgroundDrawable(null);
									}
								}
								checkOperator(mFactory.mOperator);
							}
							}
							else{
								if (a > 535 * DEVICE_WIDTH / 1280
										&& a < 620 * DEVICE_WIDTH / 1280
										&& b > 330 * DEVICE_HEIGHT / 800
										&& b < 435 * DEVICE_HEIGHT / 800
										|| a > 940 * DEVICE_WIDTH / 1280
										&& a < 1020 * DEVICE_WIDTH / 1280
										&& b > 330 * DEVICE_HEIGHT / 800
										&& b < 435 * DEVICE_HEIGHT / 800) {
									if (a > 535 * DEVICE_WIDTH / 1280
											&& a < 620 * DEVICE_WIDTH / 1280
											&& b > 330 * DEVICE_HEIGHT / 800
											&& b < 435 * DEVICE_HEIGHT / 800) {
										mGameActivity.setLayoutParamView(lp,
												arrImageItems[status],
												535 * DEVICE_WIDTH / 1280,
												345 * DEVICE_HEIGHT / 800);
										count1 = status + 1;
										txtParam2.setBackgroundDrawable(null);

									}
									if (a > 940 * DEVICE_WIDTH / 1280
											&& a < 1020 * DEVICE_WIDTH / 1280
											&& b > 330 * DEVICE_HEIGHT / 800
											&& b < 435 * DEVICE_HEIGHT / 800) {
										mGameActivity.setLayoutParamView(lp,
												arrImageItems[status],
												940 * DEVICE_WIDTH / 1280,
												345 * DEVICE_HEIGHT / 800);
										count2 = status + 1;
										txtResult.setBackgroundDrawable(null);

									}
								}
								checkOperator(mFactory.mOperator);
							}

							break;
							case 2:

								 break;
						}

					}
				}

				return true;
			}
		});

	}
	public void onDraw(){
		for ( int i = 0; i < 9; i++) {
			mGameActivity.setLayoutParamView(lp, arrImageItems[i], arrX[i],
					arrY[i]);
			arrImageItems[i].setOnTouchListener(this); 

		}
		
		mGameActivity.setLayoutParamView(lp, txtHienthimanchoi,
				425 * DEVICE_WIDTH / 1280, 0);
		mGameActivity.setLayoutParamView(lp, rlbackhome, x / 9, y / 9);
		mGameActivity.setLayoutParamView(lp, txtScore,
				1017 * DEVICE_WIDTH / 1280, 45 * DEVICE_HEIGHT / 800);
		mGameActivity.setLayoutParamView(lp, txtTodayBestscore,
				1017 * DEVICE_WIDTH / 1280, 133 * DEVICE_HEIGHT / 800);
		
		if(mFactory.getDifficultLevel()==0||mFactory.mDifficultLevel==1){
			mGameActivity.setLayoutParamView(lp, lnParam1, 149*DEVICE_WIDTH/1280, 334*DEVICE_HEIGHT/800);
			
			mGameActivity.setLayoutParamView(lp, txtOperator1, 331*DEVICE_WIDTH/1280,334*DEVICE_HEIGHT/800 );
			mGameActivity.setLayoutParamView(lp, lnParam2, 515*DEVICE_WIDTH/1280, 334*DEVICE_HEIGHT/800);
			mGameActivity.setLayoutParamView(lp, txtBang, 701*DEVICE_WIDTH/1280,334*DEVICE_HEIGHT/800 );
			mGameActivity.setLayoutParamView(lp, lnResult, 894*DEVICE_WIDTH/1280, 334*DEVICE_HEIGHT/800);
		}
		if(mFactory.getDifficultLevel()==2){
			mGameActivity.setLayoutParamView(lp, lnParam1, 121*DEVICE_WIDTH/1280, 343*DEVICE_HEIGHT/800);
			
			mGameActivity.setLayoutParamView(lp, txtOperator1, 250*DEVICE_WIDTH/1280,343*DEVICE_HEIGHT/800 );
			mGameActivity.setLayoutParamView(lp, lnParam2, 404*DEVICE_WIDTH/1280, 343*DEVICE_HEIGHT/800);
			mGameActivity.setLayoutParamView(lp, txtOperator2, 560*DEVICE_WIDTH/1280, 343*DEVICE_HEIGHT/800);
			mGameActivity.setLayoutParamView(lp, lnParam3, 723*DEVICE_WIDTH/1280, 343*DEVICE_HEIGHT/800);
			mGameActivity.setLayoutParamView(lp, txtBang, 878*DEVICE_WIDTH/1280,343*DEVICE_HEIGHT/800 );
			mGameActivity.setLayoutParamView(lp, lnResult, 969*DEVICE_WIDTH/1280, 343*DEVICE_HEIGHT/800);
			
			
		}
		
	}

	public String getTextBoard(int i, String str) {
		switch (i) {
		case 0:
			str = "Addition";
			break;
		case 1:
			str = "Subtraction";
			break;
		case 2:
			str = "Multiplication";
			break;
		case 3:
			str = "Division";
			break;
		case 4:
			str = "Mix";
			break;

		default:
			break;
		}
		return str;
	}

	public void setLayoutParamView(android.view.ViewGroup.LayoutParams lp,
			View mView, int width, int heigth) {
		lp = new AbsoluteLayout.LayoutParams(
				android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
				android.view.ViewGroup.LayoutParams.WRAP_CONTENT, width, heigth);

		mView.setLayoutParams(lp);

	}

	public int getWidth() {
		return x;
	}

	public int getHeight() {
		return y;
	}

	public void checkOperator(int Operat) {
		switch (Operat) {
		case 0:
			checkAdd();
			break;
		case 1:
			checkSub();
			break;
		case 2:
			checkMulti();

			break;
		case 3:
			checkDivis();
			break;
		case 4:
			checkMix();
			break;

		default:
			break;
		}

	}

	public void checkAdd() {

		switch (mFactory.getDifficultLevel()) {
		case 0:

			if (mFactory.getParam1() == -1 && status != -1) {
				int i = (status + 1) + mFactory.getParam2();
				if (i == mFactory.getResult())
					onCorrect();
				else
					inCorrect();
			}
			if (mFactory.getParam1() != -1 && status != -1) {
				int i = (status + 1) + mFactory.getParam1();
				if (i == mFactory.getResult())
					onCorrect();

				else
					inCorrect();

			}
			break;
		case 1:

			if (status != -1) {
				int i = count1 + count2;
				if (i == mFactory.getResult())
					onCorrect();
				else
					inCorrect();
			}

			break;
			case 2:
				if(status !=-1){
					if(mFactory.getParam1()!=-1){
						int i = count1 + count2+mFactory.getParam1();
						if(i==mFactory.getResult())
							onCorrect();

							else
								inCorrect();
						
					}
					if(mFactory.getParam2()!=-1){
						int i = count1 + count2+mFactory.getParam2();
						if(i==mFactory.getResult())
							onCorrect();

							else
								inCorrect();
						
					}
					if(mFactory.getParam3()!=-1){
						int i = count1 + count2+mFactory.getParam2();
						if(i==mFactory.getResult())
							onCorrect();

							else
								inCorrect();
	
					}
				}
				break;

		}
	}

	public void checkSub() {

		switch (mFactory.getDifficultLevel()) {
		case 0:
			if (mFactory.getParam1() == -1 && status != -1) {
				int i = mGameActivity.getValue(status) - mFactory.getParam2();
				if (i == mFactory.getResult())
					onCorrect();

				else
					inCorrect();
			}
			if (mFactory.getParam1() != -1 && status != -1) {
				int i = -mGameActivity.getValue(status) + mFactory.getParam1();
				if (i == mFactory.getResult())
					onCorrect();

				else
					inCorrect();

			}
			break;
		case 1:
			if (status != -1) {
				int i = count1 - count2;
				if (i == mFactory.getResult())
					onCorrect();
				else
					inCorrect();
			}
			break;

		}

	}

	public void checkMulti() {

		switch (mFactory.getDifficultLevel()) {
		case 0:

			if (mFactory.getParam1() == -1 && status != -1) {
				int i = mGameActivity.getValue(status) * mFactory.getParam2();
				if (i == mFactory.getResult())
					onCorrect();

				else
					inCorrect();
			}
			if (mFactory.getParam1() != -1 && status != -1) {
				int i = mGameActivity.getValue(status) * mFactory.getParam1();
				if (i == mFactory.getResult())
					onCorrect();

				else
					inCorrect();

			}
			break;
		case 1:
			if (status != -1) {
				int i = count1 * count2;
				if (i == mFactory.getResult())
					onCorrect();
				else
					inCorrect();
			}
			break;

		}
	}

	public void checkDivis() {

		switch (mFactory.getDifficultLevel()) {
		case 0:

			if (mFactory.getParam2() == -1 && status != -1) {
				int i = mGameActivity.getValue(status) * mFactory.getResult();
				if (i == mFactory.getParam1())
					onCorrect();

				else
					inCorrect();
			}
			if (mFactory.getParam2() != -1 && status != -1) {
				int i = mGameActivity.getValue(status) * mFactory.getParam2();
				if (i == mFactory.getParam1())
					onCorrect();

				else
					inCorrect();

			}
			break;
		case 1:
			if (status != -1) {
				int i = count1 * count2;
				if (i == mFactory.getParam1())
					onCorrect();
				else
					inCorrect();
			}

			break;

		}

	}

	public void checkMix() {

		switch (mFactory.getDifficultLevel()) {
		case 0:
			switch (mFactory.mOperatorMix) {
			case 1:

				if (mFactory.getParam1() == -1 && status != -1) {
					int i = (status + 1) + mFactory.getParam2();
					if (i == mFactory.getResult())
						onCorrect();
					else
						inCorrect();
				}
				if (mFactory.getParam1() != -1 && status != -1) {
					int i = (status + 1) + mFactory.getParam1();
					if (i == mFactory.getResult())
						onCorrect();

					else
						inCorrect();

				}
				break;
			case 2:
				if (mFactory.getParam1() == -1 && status != -1) {
					int i = mGameActivity.getValue(status) - mFactory.getParam2();
					if (i == mFactory.getResult())
						onCorrect();

					else
						inCorrect();
				}
				if (mFactory.getParam1() != -1 && status != -1) {
					int i = -mGameActivity.getValue(status) + mFactory.getParam1();
					if (i == mFactory.getResult())
						onCorrect();

					else
						inCorrect();

				}
				break;
			case 3:

				if (mFactory.getParam1() == -1 && status != -1) {
					int i = mGameActivity.getValue(status) * mFactory.getParam2();
					if (i == mFactory.getResult())
						onCorrect();

					else
						inCorrect();
				}
				if (mFactory.getParam1() != -1 && status != -1) {
					int i = mGameActivity.getValue(status) * mFactory.getParam1();
					if (i == mFactory.getResult())
						onCorrect();

					else
						inCorrect();

				}
				break;
			case 4:

				if (mFactory.getParam2() == -1 && status != -1) {
					int i = mGameActivity.getValue(status) * mFactory.getResult();
					if (i == mFactory.getParam1())
						onCorrect();

					else
						inCorrect();
				}
				if (mFactory.getParam2() != -1 && status != -1) {
					int i = mGameActivity.getValue(status) * mFactory.getParam2();
					if (i == mFactory.getParam1())
						onCorrect();

					else
						inCorrect();

				}
				break;
			}
			break;
		case 1:
			switch (mFactory.mOperatorMix) {
			case 1:
				if (status != -1) {
					int i = count1 + count2;
					if (i == mFactory.getResult())
						onCorrect();
					else
						inCorrect();
				}
				break;

			case 2:
				if (status != -1) {
					int i = count1 - count2;
					if (i == mFactory.getResult())
						onCorrect();
					else
						inCorrect();
				}
				break;
			case 3:
				if (status != -1) {
					int i = count1 * count2;
					if (i == mFactory.getResult())
						onCorrect();
					else
						inCorrect();
				}
				break;
			case 4:
				if (status != -1) {
					int i = count1 * count2;
					if (i == mFactory.getParam1())
						onCorrect();
					else
						inCorrect();
				}
				break;
			}
			break;

		}
		
		

	}

	public int getValue(int status) {

		return status + 1;

	}

	private void onCorrect() {
		onCorrect = true;
		oninCorrect = false;
		mGameActivity.setLayoutParamView(lp, arrImageItems[status],
				arrX[status], arrY[status]);
		if (mFactory.mDifficultLevel == 1) {
			mGameActivity.setLayoutParamView(lp, arrImageItems[count1 - 1],
					arrX[count1 - 1], arrY[count1 - 1]);
			mGameActivity.setLayoutParamView(lp, arrImageItems[count2 - 1],
					arrX[count2 - 1], arrY[count2 - 1]);
		}
		count1 = count2 = 0;
		nextQuestion();
	}

	private void inCorrect() {
		oninCorrect = true;
		onCorrect = false;

	}

	public void nextQuestion() {
		mFactory.nextQuestion();
		
		if(mFactory.getDifficultLevel()==0){
			if(mFactory.mOperator!=3){
			if(mFactory.mOperatorMix!=4){
			lnParam3.setBackgroundDrawable(null);
			txtParam3.setBackgroundDrawable(null);
			txtOperator2.setBackgroundDrawable(null);
			if(mFactory.getParam1()!=-1){
				lnParam1.setBackgroundDrawable(null);
				txtParam1.setBackgroundDrawable(null);
				txtParam1.setText(""+mFactory.getParam1());
				lnParam2.setBackgroundResource(R.drawable.khung);
				txtParam2.setBackgroundResource(R.drawable.dau_hoi);
				txtParam2.setText(null);
			}
			if(mFactory.getParam2()!=-1){
				lnParam1.setBackgroundResource(R.drawable.khung);
				txtParam1.setBackgroundResource(R.drawable.dau_hoi);
				txtParam1.setText(null);
				lnParam2.setBackgroundDrawable(null);
				txtParam2.setBackgroundDrawable(null);
				txtParam2.setText(""+mFactory.getParam2());
			}

				lnResult.setBackgroundDrawable(null);
				txtResult.setBackgroundDrawable(null);
				txtResult.setText(""+mFactory.getResult());
			}else{
				lnParam3.setBackgroundDrawable(null);
				txtParam3.setBackgroundDrawable(null);
				txtOperator2.setBackgroundDrawable(null);

				lnParam1.setBackgroundDrawable(null);
				txtParam1.setBackgroundDrawable(null);
				txtParam1.setText(""+mFactory.getParam1());
				if(mFactory.getParam2()!=-1){
					lnParam2.setBackgroundDrawable(null);
					txtParam2.setBackgroundDrawable(null);
					txtParam2.setText(""+mFactory.getParam2());
					lnResult.setBackgroundResource(R.drawable.khung);
					txtResult.setBackgroundResource(R.drawable.dau_hoi);
					txtResult.setText(null);
				}
				if(mFactory.getResult()!=-1){
					lnParam2.setBackgroundResource(R.drawable.khung);
					txtParam2.setBackgroundResource(R.drawable.dau_hoi);
					txtParam2.setText(null);
					lnResult.setBackgroundDrawable(null);
					txtResult.setBackgroundDrawable(null);
					txtResult.setText(""+mFactory.getResult());
				}
				
			}
		}else{
			lnParam3.setBackgroundDrawable(null);
			txtParam3.setBackgroundDrawable(null);
			txtOperator2.setBackgroundDrawable(null);

			lnParam1.setBackgroundDrawable(null);
			txtParam1.setBackgroundDrawable(null);
			txtParam1.setText(""+mFactory.getParam1());
			if(mFactory.getParam2()!=-1){
				lnParam2.setBackgroundDrawable(null);
				txtParam2.setBackgroundDrawable(null);
				txtParam2.setText(""+mFactory.getParam2());
				lnResult.setBackgroundResource(R.drawable.khung);
				txtResult.setBackgroundResource(R.drawable.dau_hoi);
				txtResult.setText(null);
			}
			if(mFactory.getResult()!=-1){
				lnParam2.setBackgroundResource(R.drawable.khung);
				txtParam2.setBackgroundResource(R.drawable.dau_hoi);
				txtParam2.setText(null);
				lnResult.setBackgroundDrawable(null);
				txtResult.setBackgroundDrawable(null);
				txtResult.setText(""+mFactory.getResult());
			}

			
			
		}
		
			switch (mFactory.mOperator) {
			case 0:
				txtOperator1.setBackgroundResource(R.drawable.cong);
				break;
			case 1:
				txtOperator1.setBackgroundResource(R.drawable.tru);
				break;
			case 2:
				txtOperator1.setBackgroundResource(R.drawable.nhan);
				break;
			case 3:
				txtOperator1.setBackgroundResource(R.drawable.chia);
				break;
			case 4:
				switch (mFactory.mOperatorMix) {
				case 1:
					txtOperator1.setBackgroundResource(R.drawable.cong);

					break;
				case 2:
					txtOperator1.setBackgroundResource(R.drawable.tru);

					break;
				case 3:
					txtOperator1.setBackgroundResource(R.drawable.nhan);

					break;
				case 4:
					txtOperator1.setBackgroundResource(R.drawable.chia);

					break;

				}
				
				break;
				

			}
			
		}	
				
		
		if(mFactory.getDifficultLevel()==1){
			lnParam3.setBackgroundDrawable(null);
			txtParam3.setBackgroundDrawable(null);
			txtOperator2.setBackgroundDrawable(null);
			if(mFactory.mOperator!=3){
			if(mFactory.mOperatorMix!=4){
			txtParam1.setText(null);
			lnParam1.setBackgroundResource(R.drawable.khung);
			txtParam1.setBackgroundResource(R.drawable.dau_hoi);
			txtParam2.setText(null);
			lnParam2.setBackgroundResource(R.drawable.khung);
			txtParam2.setBackgroundResource(R.drawable.dau_hoi);
			lnResult.setBackgroundDrawable(null);
			txtResult.setBackgroundDrawable(null);
			txtResult.setText(""+mFactory.getResult());
			}else{
				txtResult.setText(null);
				lnResult.setBackgroundResource(R.drawable.khung);
				txtResult.setBackgroundResource(R.drawable.dau_hoi);
				txtParam2.setText(null);
				lnParam2.setBackgroundResource(R.drawable.khung);
				txtParam2.setBackgroundResource(R.drawable.dau_hoi);
				lnParam1.setBackgroundDrawable(null);
				txtParam1.setBackgroundDrawable(null);
				txtParam1.setText(""+mFactory.getParam1());	
			}
			}else{
			txtResult.setText(null);
			lnResult.setBackgroundResource(R.drawable.khung);
			txtResult.setBackgroundResource(R.drawable.dau_hoi);
			txtParam2.setText(null);
			lnParam2.setBackgroundResource(R.drawable.khung);
			txtParam2.setBackgroundResource(R.drawable.dau_hoi);
			lnParam1.setBackgroundDrawable(null);
			txtParam1.setBackgroundDrawable(null);
			txtParam1.setText(""+mFactory.getParam1());
			
			}
			switch (mFactory.mOperator) {
			case 0:
				txtOperator1.setBackgroundResource(R.drawable.cong);
				break;
			case 1:
				txtOperator1.setBackgroundResource(R.drawable.tru);
				break;
			case 2:
				txtOperator1.setBackgroundResource(R.drawable.nhan);
				break;
			case 3:
				txtOperator1.setBackgroundResource(R.drawable.chia);
				break;
			case 4:
				switch (mFactory.mOperatorMix) {
				case 1:
					txtOperator1.setBackgroundResource(R.drawable.cong);

					break;
				case 2:
					txtOperator1.setBackgroundResource(R.drawable.tru);

					break;
				case 3:
					txtOperator1.setBackgroundResource(R.drawable.nhan);

					break;
				case 4:
					txtOperator1.setBackgroundResource(R.drawable.chia);

					break;

				}
				break;
				

			}
			
			
		}
		if(mFactory.getDifficultLevel()==2){
			if(mFactory.mOperator!=3){
			if(mFactory.getParam1()!=-1){
				lnParam1.setBackgroundDrawable(null);
				txtParam1.setBackgroundDrawable(null);
				txtParam1.setText(""+mFactory.getParam1());
				
				lnParam2.setBackgroundResource(R.drawable.khung);
				txtParam2.setBackgroundResource(R.drawable.dau_hoi);
				txtParam2.setText(null);
				lnParam3.setBackgroundResource(R.drawable.khung);
				txtParam3.setBackgroundResource(R.drawable.dau_hoi);
				txtParam3.setText(null);
			}
			if(mFactory.getParam2()!=-1){
				lnParam2.setBackgroundDrawable(null);
				txtParam2.setBackgroundDrawable(null);
				txtParam2.setText(""+mFactory.getParam2());
				lnParam1.setBackgroundResource(R.drawable.khung);
				txtParam1.setBackgroundResource(R.drawable.dau_hoi);
				txtParam1.setText(null);
				lnParam3.setBackgroundResource(R.drawable.khung);
				txtParam3.setBackgroundResource(R.drawable.dau_hoi);
				txtParam3.setText(null);
			}
			if(mFactory.getParam3()!=-1){
				lnParam3.setBackgroundDrawable(null);
				txtParam3.setBackgroundDrawable(null);
				txtParam3.setText(""+mFactory.getParam3());
				lnParam2.setBackgroundResource(R.drawable.khung);
				txtParam2.setBackgroundResource(R.drawable.dau_hoi);
				txtParam2.setText(null);
				lnParam1.setBackgroundResource(R.drawable.khung);
				txtParam1.setBackgroundResource(R.drawable.dau_hoi);
				txtParam1.setText(null);
			}
				lnResult.setBackgroundDrawable(null);
				txtResult.setBackgroundDrawable(null);
				txtResult.setText(""+mFactory.getResult());
			}else{
				lnParam1.setBackgroundDrawable(null);
				txtParam1.setBackgroundDrawable(null);
				txtParam1.setText(""+mFactory.getParam1());
				if(mFactory.getParam2()!=-1){
					lnParam2.setBackgroundDrawable(null);
					txtParam2.setBackgroundDrawable(null);
					txtParam2.setText(""+mFactory.getParam2());
					lnResult.setBackgroundResource(R.drawable.khung);
					txtResult.setBackgroundResource(R.drawable.dau_hoi);
					txtResult.setText(null);
					lnParam3.setBackgroundResource(R.drawable.khung);
					txtParam3.setBackgroundResource(R.drawable.dau_hoi);
					txtParam3.setText(null);
					
				}
				if(mFactory.getParam3()!=-1){
					lnParam3.setBackgroundDrawable(null);
					txtParam3.setBackgroundDrawable(null);
					txtParam3.setText(""+mFactory.getParam3());
					lnParam2.setBackgroundResource(R.drawable.khung);
					txtParam2.setBackgroundResource(R.drawable.dau_hoi);
					txtParam2.setText(null);
					lnResult.setBackgroundResource(R.drawable.khung);
					txtResult.setBackgroundResource(R.drawable.dau_hoi);
					txtResult.setText(null);
				}
				if(mFactory.getResult()!=-1){
					lnResult.setBackgroundDrawable(null);
					txtResult.setBackgroundDrawable(null);
					txtResult.setText(""+mFactory.getResult());
					lnParam2.setBackgroundResource(R.drawable.khung);
					txtParam2.setBackgroundResource(R.drawable.dau_hoi);
					txtParam2.setText(null);
					lnParam3.setBackgroundResource(R.drawable.khung);
					txtParam3.setBackgroundResource(R.drawable.dau_hoi);
					txtParam3.setText(null);
				}
				
			}
			switch (mFactory.mOperator) {
			case 0:
				txtOperator1.setBackgroundResource(R.drawable.cong);
				txtOperator2.setBackgroundResource(R.drawable.cong);
				break;
			case 1:
				txtOperator1.setBackgroundResource(R.drawable.tru);
				txtOperator2.setBackgroundResource(R.drawable.tru);
				break;
			case 2:
				txtOperator1.setBackgroundResource(R.drawable.nhan);
				txtOperator2.setBackgroundResource(R.drawable.tru);
				break;
			case 3:
				txtOperator1.setBackgroundResource(R.drawable.chia);
				txtOperator2.setBackgroundResource(R.drawable.tru);
				break;
			case 4:
				
				break;
					
			
		}
		

	}
	}

	public boolean onTouch(View v, MotionEvent event) {
		switch (v.getId()) {
		case R.id.imgSo1:
			status = 0;
			break;
		case R.id.imgSo2:
			status = 1;
			break;
		case R.id.imgSo3:
			status = 2;
		
			break;
		case R.id.imgSo4:
			status = 3;

			break;
		case R.id.imgSo5:
			status = 4;

			break;
		case R.id.imgSo6:
			status = 5;

			break;
		case R.id.imgSo7:
			status = 6;

			break;
		case R.id.imgSo8:
			status = 7;
			
			break;
		case R.id.imgSo9:
			status = 8;			
			break;
		}

		if (event.getAction() == MotionEvent.ACTION_UP) {
			status = -1;
		}
		
		return false;
	}

	public void onClick(View v) {
		if (v.getId() == R.id.btnback) {

			Intent i = new Intent(GameActivity.this,OperatorSelected.class);
			startActivity(i);
			finish();
			mFactory.mCurrentQuestionNo = 0;
		}
		
	}

}
